<template>
	<view class="app">
		<!-- 页面内容开始 -->
		<view class="avatar-box">
			<button size="mini" class="avatar-button" plain open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
				<view class="avatar-image-box">
					<image class="avatar-image" :src="submitdata.avatar || avatar || defaultAvatarUrl"></image>
				</view>
			</button>
		</view>


		<!-- 昵称输入 -->
		<view class="nickname-box">
			<view class="nickname">
				<input class="input" type="nickname" v-model="submitdata.nickname" placeholder="请输入昵称"
					placeholder-style="color: #AAAAAA;" :change="getNickname">
			</view>
		</view>

		<button type="primary" @click="updateUserInfo" style="margin-top: 40rpx;">{{submitText}}</button>
		<!-- 页面内容结束 -->
	</view>
</template>

<script>
	const defaultAvatarUrl =
		''

	import userStore from '../../utils/userStore.js'
	export default {
		name: "set-user-info",
		props: {
			submitText: {
				type: String,
				default: '修改'
			}
		},
		data() {
			// 页面数据变量
			return {
				avatar: '',
				defaultAvatarUrl,
				submitdata: {}
			}
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {

		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady() {},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {
			this.init();
		},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {},
		// 函数
		methods: {
			// 页面数据初始化函数
			init() {
				this.userInfo = userStore.get();
				this.submitdata.avatar = userStore.get().logo;
				this.submitdata.nickname = userStore.get().nickname;

			},
			getNickname: function(e) {
				this.submitdata.nickname = e.detail.value;
				console.log(this.submitdata.nickname)

			},
			onChooseAvatar(e) {
				const {
					avatarUrl
				} = e.detail;
				this.avatar = avatarUrl;
			},
			async updateUserInfo() {
				// 上传图片到云储存
				let _this = this;
				if (_this.avatar) {
					uni.uploadFile({
						url: 'https://zm.quya.vip/api/v1/uploadFile', //仅为示例，非真实的接口地址
						filePath: _this.avatar,
						name: 'file',
						success: (uploadFileRes) => {
							let res = JSON.parse(uploadFileRes.data);
							console.log(res.data.url);
							_this.submitdata.avatar = res.data.url;


							_this.emitdata(_this.submitdata);
						}
					});
				} else {

					_this.emitdata(_this.submitdata);
				}

			},
			emitdata(data) {
				// if (!data.avatar || !data.nickname) {
				// 	uni.showToast({
				// 		title:'请填写昵称或头像',
				// 		icon:'none'
				// 	})
				// } else {
					this.$emit('updateUserInfo', data);
				// }

			}
		},
		// 监听器
		watch: {

		},
		// 计算属性
		computed: {

		}
	}
</script>
<style lang="scss" scoped>
	.app {
		padding: 30rpx;
	}

	button {
		color: #fff;
		background-color: #609af2;
		height: 100upx;
		border-radius: 50upx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.avatar-box {
		/* #ifndef APP-NVUE */
		display: flex;
		justify-content: center;

		/* #endif */
		.avatar-button {
			border: 0;
			display: contents;
		}

		.avatar-image-box {
			width: 120rpx;
			height: 120rpx;
			background-color: #f3f3f3;
			border-radius: 50%;
			overflow: hidden;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.avatar-image {
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
			overflow: hidden;
		}
	}

	/* 昵称 */
	.nickname-box {
		margin-top: 40rpx;

		.nickname {
			margin-top: 16rpx;
			width: 100%;
			padding: 26rpx 20rpx;
			border-radius: 10rpx;
			background-color: #F8F7F8;
		}

		.input {
			color: #080808;
		}
	}
</style>